#include "gfx_string.h"

#include "gfx_sprite.h"

GFX_String* gfx_string_create(const char *image, const char *text)
{
    GFX_String *string= mem_allocate(sizeof(GFX_String));
    GFX_Image *nom=gfx_image_get(image);
    string->image=nom;
    string->x=0;
    string->y=0;
    string->text=mem_strdup(text);
    return string;
}

void gfx_string_delete(GFX_String *string)
{
    if(string == 0) return;
    mem_free(string->text);
    mem_free(string);
}

void gfx_string_set_text(GFX_String *s, const char *text)
{
    mem_free(s->text);
    s->text=mem_strdup(text);
}

void gfx_render_string(GFX_String *string)
{
    if(string == 0) { return; }
    if(string->image == 0)
    {
        log_puts(LOG_WARN, "Tried to render a GFX_String without an image");
        return;
    }
    GFX_Sprite sprite;
    sprite.x = string->x;
    sprite.y = string->y;
    sprite.image = string->image;

    for(const char *str = string->text; *str; str++)
    {
        sprite.frame = *str;
        gfx_render_sprite(&sprite);
        sprite.x += string->image->width  / string->image->columns;
    }
    return;
}
